/**
 * @author Created by qiush on 2017-06-09.
 */

const {dialog} = require('electron');

const dialogUtil = {

  showOpen(options) {
    return new Promise((resolve, reject) => {
      dialog.showOpenDialog(options, filePaths => {
        if (filePaths && filePaths.length > 0) {
          resolve(filePaths[0]);
        } else {
          reject();
        }
      });
    });
  },

  showSave(options) {
    return new Promise((resolve, reject) => {
      dialog.showSaveDialog(options, filename => {
        if (filename) {
          resolve(filename);
        } else {
          reject();
        }
      });
    });
  },

  showError(msg) {
    dialog.showErrorBox('出错了', msg);
  },

  showMessage(browserWindow, msg) {
    return new Promise((resolve) => {
      const options = {
        type: 'info',
        buttons: ['确定'],
        title: '提示',
        message: msg,
      };
      const callback = (response) => {
        resolve(response);
      };
      dialog.showMessageBox(browserWindow, options, callback);
    });
  }

};

module.exports = dialogUtil;
